package sicau.carManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import sicau.carManager.entity.FixedCarInformation;
import sicau.carManager.mapper.FixedCarInformationMapper;
import sicau.carManager.service.FixedCarInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2021-02-05
 */
@Service
public class FixedCarInformationServiceImpl extends ServiceImpl<FixedCarInformationMapper, FixedCarInformation> implements FixedCarInformationService {

    @Autowired
    FixedCarInformationMapper fixMapper;

    @Override
    public FixedCarInformation selectCarByPlateNum(String plateNum, String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("license_plate_number", plateNum);
        queryWrapper.eq("campus", campus);
        return fixMapper.selectOne(queryWrapper);
    }

    @Override
    public List<FixedCarInformation> selectCarByName(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("applicant", name);
        List<FixedCarInformation> fixedCarInformations = fixMapper.selectList(queryWrapper);
        return fixedCarInformations;
    }

    @Override
    public List<FixedCarInformation> selectAllFixedCarInformation(String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("campus", campus);
        return fixMapper.selectList(queryWrapper);
    }

    @Override
    public int updateByPlateNum(FixedCarInformation carInformation) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("campus", carInformation.getCampus());
        updateWrapper.eq("license_plate_number", carInformation.getLicensePlateNumber());
        return fixMapper.update(carInformation, updateWrapper);
    }

    @Override
    public String getPhoneNumberByLicensePlate(String licensePlate) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("license_plate_number", licensePlate);
        queryWrapper.select("phone_number");
        FixedCarInformation information = fixMapper.selectOne(queryWrapper);
        if (information == null) {
            return null;
        }
        return information.getPhoneNumber();
    }


}
